package com.xy.lang.util;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class CalendarUtils {

    public final static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

    public static String format(LocalDateTime localDateTime) {
        return format(localDateTime, DEFAULT_PATTERN);
    }

    public static String format(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDateTime parse(String value) {
        return parse(value, DEFAULT_PATTERN);
    }

    public static LocalDateTime parse(String value, String pattern) {
        return LocalDateTime.parse(value, DateTimeFormatter.ofPattern(pattern));
    }

    public static String formatDate(LocalDate localDate) {
        return formatDate(localDate, DEFAULT_DATE_PATTERN);
    }

    public static String formatDate(LocalDate localDate, String pattern) {
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate parseDate(String value) {
        return parseDate(value, DEFAULT_DATE_PATTERN);
    }

    public static LocalDate parseDate(String value, String pattern) {
        return LocalDate.parse(value, DateTimeFormatter.ofPattern(pattern));
    }

    public static Duration duration(LocalDateTime offset, LocalDateTime deadline) {
        LocalDateTime offset1 = offset == null ? LocalDateTime.now() : offset;
        LocalDateTime deadline1 = deadline == null ? LocalDateTime.now() : deadline;
        return Duration.between(offset1, deadline1);
    }

    public static String getDeadlineString(LocalDateTime offset) {
        if (offset == null) {
            return "";
        }
        return getDeadlineString(offset, LocalDateTime.now());
    }

    public static String getDeadlineString(LocalDateTime offset, LocalDateTime deadline) {
        if (offset == null || deadline == null) {
            return "";
        }
        Duration duration = duration(offset, deadline);
        String str = "";
        long value;
        if (!duration.isNegative()) {
            if ((value = duration.getSeconds()) < 60) {
                str = (value + 1) + "秒前";
            } else if ((value = duration.toMinutes()) > 0 && value < 60) {
                str = value + "分钟前";
            } else if ((value = duration.toHours()) > 0 && value < 24) {
                str = value + "小时前";
            } else if ((value = duration.toDays()) > 0 && value < 30) {
                str = value + "天前";
            } else {
                str = formatDate(deadline.toLocalDate());
            }
        }
        return str;
    }

    public static LocalDateTime getStartTime(LocalDateTime value) {
        return getStartTime(value, 0);
    }

    public static LocalDateTime getStartTime(LocalDateTime value, int days) {
        if (value == null) {
            return null;
        }

        return LocalDate.from(value)
                .plusDays(days)
                .atTime(LocalTime.of(0, 0, 0));
    }

    public static LocalDateTime getEndTime(LocalDateTime value) {
        return getEndTime(value, 0);
    }

    public static LocalDateTime getEndTime(LocalDateTime value, int days) {
        if (value == null) {
            return null;
        }

        return LocalDate.from(value)
                .plusDays(days)
                .atTime(LocalTime.of(23, 59, 59));
    }

    public static long toEpochSecond(LocalDateTime localDateTime) {
        if (null == localDateTime) {
            return 0L;
        }
        return localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
    }

    public static long toEpochSecond() {
        return toEpochSecond(LocalDateTime.now());
    }

    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(getStartTime(now, 0));
        System.out.println(getEndTime(now, 0));
        System.out.println(getStartTime(now, -7));
        System.out.println(getEndTime(now, -1));
        System.out.println(getStartTime(now, 1));
        System.out.println(getEndTime(now, 7));
        System.out.println(toEpochSecond());
        System.out.println(toEpochSecond(LocalDateTime.of(2070, 1, 1, 0, 0, 0)));
        System.out.println(formatDate(LocalDate.now()));
        System.out.println(parseDate("2021-03-03"));
        System.out.println(System.currentTimeMillis());
        String s = formatSecond(5934);
        System.out.println(s);
    }

    public static String formatSecond(Object second) {
        String time = "0秒";
        if (second != null) {
            int s = (int) second;
            String format;
            Object[] array;
            Integer hours = (s / (60 * 60));
            Integer minutes = (s / 60 - hours * 60);
            Integer seconds = (s - minutes * 60 - hours * 60 * 60);
            if (hours > 0) {
                format = "%1$,d时%2$,d分%3$,d秒";
                array = new Object[]{hours, minutes, seconds};
            } else if (minutes > 0) {
                format = "%1$,d分%2$,d秒";
                array = new Object[]{minutes, seconds};
            } else {
                format = "%1$,d秒";
                array = new Object[]{seconds};
            }
            time = String.format(format, array);
        }
        return time;

    }

}
